package io.tec.cloud.algorithm.c04_v20220613;

import io.tec.cloud.algorithm.AlgorithmUtil;

/**
 * https://www.processon.com/view/link/62b9bcdde0b34d075b0fbb75
 * <p>
 * 所有数的小和累加起来，叫数组小和
 * 例子： [1,3,4,2,5]
 * 1左边比1小的数：没有
 * 3左边比3小的数：1
 * 4左边比4小的数：1、3
 * 2左边比2小的数：1
 * 5左边比5小的数：1、3、4、 2
 * 所以数组的小和为1+1+3+1+1+3+4+2=16
 * 给定一个数组arr，求数组小和
 */
public class T28_SmallSum {

    /**
     * 递归版本
     * @param arr
     * @return
     */
    public static int smallSum1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }

        return process(arr, 0, arr.length - 1);
    }

    private static int process(int[] arr, int l, int r) {
        // base case
        if (l == r) {
            return 0;
        }
        int mid = l + ((r - l) >> 1);
        return process(arr, l, mid) + process(arr, mid + 1, r) + merge(arr, l, mid, r);
    }

    private static int merge(int[] arr, int l, int mid, int r) {
        int li = l;
        int ri = mid + 1;

        int[] help = new int[r - l + 1];
        int idx = 0;
        int sum = 0;
        while (li <= mid && ri <= r) {
            sum += arr[li] < arr[ri] ? arr[li] * (r - ri + 1) : 0;
            help[idx++] = arr[li] < arr[ri] ? arr[li++] : arr[ri++];
        }
        while (li <= mid) {
            help[idx++] = arr[li++];
        }
        while (ri <= r) {
            help[idx++] = arr[ri++];
        }

        for (int i = 0; i < help.length; i++) {
            arr[l + i] = help[i];
        }
        return sum;
    }

    /**
     * 迭代版本
     * @param arr
     * @return
     */
    public static int smallSum2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }

        int mergeSize = 1;
        int N = arr.length - 1;
        int sum = 0;
        // TODO 边界条件：数组有2个元素，mergeSize = 1;
        while (mergeSize <= N) {
            int L = 0;
            while (L <= N) {
                if (mergeSize > N - L) {
                    // TODO 结束本次循环，执行下一次循环
                    break;
                }
                int mid = L + mergeSize - 1;
                int R = mid + Math.min(N - mid, mergeSize);
                sum += merge(arr, L, mid, R);
                L = R + 1;
            }
            mergeSize *= 2;
        }
        return sum;
    }

    /**
     * 暴力版本
     * @param arr
     * @return
     */
    public static int smallSum3(int[] arr) {
        int sum = 0;
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < i; j++) {
                sum += arr[j] < arr[i] ? arr[j] : 0;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int testTimes = 100_0000;
        int maxValue = 100;
        int maxLength = 3;
        for (int t = 0; t < testTimes; t++) {
            int[] arr = AlgorithmUtil.generateArray(maxValue, AlgorithmUtil.random(maxLength));
            int[] arr1 = AlgorithmUtil.copyArray(arr);
            int[] arr2 = AlgorithmUtil.copyArray(arr);
            int[] arr3 = AlgorithmUtil.copyArray(arr);
            int sum1 = smallSum1(arr1);
            int sum2 = smallSum2(arr2);
            int sum3 = smallSum3(arr3);
            if (sum1 != sum2 || sum1 != sum3) {
                AlgorithmUtil.printArray(arr);
                System.out.println("error");
                break;
            }
        }
        System.out.println("finish");
    }
}
